package com.kintreda.ieco_server.module.account.service.impl;

import com.kintreda.ieco_server.bean.account.User;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.util.Assert;
import com.kintreda.ieco_server.util.SQLUtils;
import com.kintreda.ieco_server.util.page.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户信息管理service
 * @author HuangZhongYao
 *
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User, String> implements IUserService {

	@Autowired
	private IBaseRepository<User, String> baseRepository;

	@Override
	public PageBean<User> query(PageBean<User> page,User user,String type) {
		if ("1".equals(type)) {
			List<Object> params = new ArrayList<>();
			StringBuffer queryHql = new StringBuffer();
			boolean flag = true;

			queryHql.append("from User user");
			if (StringUtils.isNotBlank(user.getName())) {
				flag = SQLUtils.append(queryHql, flag);
				queryHql.append(" user.name like ? ");
				params.add("%" + user.getName() + "%");
			}
			if (flag){
				queryHql.append(" where type = ? or type = ? ");
				params.add("1");
				params.add("5");
			}else {
				queryHql.append(" and type = ? or type = ? ");
				params.add("1");
				params.add("5");
			}


			return super.queryPage(page, queryHql.toString(), " order by user.createDate desc ", params.toArray());
		}else{
			List<Object> params = new ArrayList<>();
			StringBuffer queryHql = new StringBuffer();
			boolean flag = true;

			queryHql.append("from User user");
			if (StringUtils.isNotBlank(user.getName())) {
				flag = SQLUtils.append(queryHql, flag);
				queryHql.append(" user.name like ? ");
				params.add("%" + user.getName() + "%");
			}
			if (StringUtils.isNotBlank(user.getMobile())) {
				flag = SQLUtils.append(queryHql, flag);
				queryHql.append(" user.mobile like ? ");
				params.add("%" + user.getMobile() + "%");
			}

			if (StringUtils.isNotBlank(user.getStatus())) {
				flag = SQLUtils.append(queryHql, flag);
				queryHql.append(" user.status = ? ");
				params.add(user.getStatus());
			}
			queryHql.append("and pid = ");
			params.add(user.getPid());
			return super.queryPage(page, queryHql.toString(), " order by user.createDate desc ", params.toArray());
		}

		}

	@Override
	public PageBean<User> queryember(PageBean<User> page, User user, String type) {
		if ("1".equals(type)) {
			List<Object> params = new ArrayList<>();
			StringBuffer queryHql = new StringBuffer();
			boolean flag = true;

			queryHql.append("from User user");
			if (StringUtils.isNotBlank(user.getName())) {
				flag = SQLUtils.append(queryHql, flag);
				queryHql.append(" user.name like ? ");
				params.add("%" + user.getName() + "%");
			}
			if (flag){
				queryHql.append(" where type = ? or type = ? or type = ? ");
				params.add("3");
				params.add("4");
				params.add("2");
			}else {
				queryHql.append(" and type = ? or type = ? or type = ?");
				params.add("3");
				params.add("4");
				params.add("2");
			}


			return super.queryPage(page, queryHql.toString(), " order by user.createDate desc ", params.toArray());
		}else{
			List<Object> params = new ArrayList<>();
			StringBuffer queryHql = new StringBuffer();
			boolean flag = true;

			queryHql.append("from User user");
			if (StringUtils.isNotBlank(user.getName())) {
				flag = SQLUtils.append(queryHql, flag);
				queryHql.append(" user.name like ? ");
				params.add("%" + user.getName() + "%");
			}
			if (StringUtils.isNotBlank(user.getMobile())) {
				flag = SQLUtils.append(queryHql, flag);
				queryHql.append(" user.mobile like ? ");
				params.add("%" + user.getMobile() + "%");
			}

			if (StringUtils.isNotBlank(user.getStatus())) {
				flag = SQLUtils.append(queryHql, flag);
				queryHql.append(" user.status = ? ");
				params.add(user.getStatus());
			}
			queryHql.append("and pid = ");
			params.add(user.getPid());
			return super.queryPage(page, queryHql.toString(), " order by user.createDate desc ", params.toArray());
		}
	}

	@Override


	public User getByAccount(String account) {

		String hql = "from User where userCode=? or mobile=? or idcard=? or name=?";
		List<User> userList = super.query(hql, new Object[]{account,account,account,account});
		return Assert.notEmpty(userList) ? userList.get(0) : null;
	}


	/**********************************************
	 * 根据状态值获取满足条件的用户信息列表
	 * @return
	 */
	public List<User> findUserList(String status) {

		String hql = " select t from User t where t.status=? and t.faceRecognition is not null and t.faceRecognition <> ?";
		return this.baseRepository.findList(hql,new Object[] {status,""});
	}

	@Override
	public User findUserByOpenId(String openId) {
		List<User> query = query("from User where openId = ? ", new Object[]{openId});
		return query.size()>0?query.get(0):null;
	}

	@Override
	public User queryByMobile(String mobile) {
		List<User> query = query("from User where mobile = ? ", new Object[]{mobile});
		return query.size()>0?query.get(0):null;
	}


	@Override
	public Boolean findByMobile(String mobile) {
		List<User> query = query("from User where mobile = ?", new Object[]{mobile});
		return query.size()>0;
	}
}
